package com.wms.controller;

import com.wms.bean.PageBean;
import com.wms.bean.ResultBean;
import com.wms.core.R;
import com.wms.dto.*;
import com.wms.entity.AllocateIn;
import com.wms.service.AllocateInService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 调拨入库控制器; (提供调拨记录的RESTful接口)
 * @author : 您的名字或参考 http://www.chiner.pro
 * @date : 2025-8-24
 */
@RestController
@RequestMapping("/allocateIn")
@Api(tags = "调拨入库管理 ---张开心")
@Slf4j
public class AllocateInController {

    @Autowired
    private AllocateInService allocateInService;

    @PostMapping("/queryPageAndCondition")
    @ApiOperation(value = "调拨入库分页查询调拨记录并按条件过滤，返回前端DTO")
    public ResultBean<PageBean<List<AllocateIn>>> queryPage(
            @ApiParam(value = "调拨查询条件", required = true) @RequestBody AllocateInDTO allocateInDTO) {
        log.info("分页查询调拨记录: 页码={}, 每页条数={}, 条件={}", allocateInDTO.getPage(), allocateInDTO.getPageSize(), allocateInDTO);
        PageBean<List<AllocateInVo>> pageBean = allocateInService.queryPageAndCondition(allocateInDTO);
        return R.success("分页查询成功", pageBean);
    }

    @PostMapping("/insert")
    @ApiOperation(value = "新增调拨记录")
    public ResultBean<Integer> insert(
            @ApiParam(value = "调拨信息", required = true) @RequestBody AllocateInUpVo allocateInUpVo) {
        log.info("新增调拨记录: {}", allocateInUpVo);
        int rows = allocateInService.insert(allocateInUpVo);
        return R.saveResult(rows);
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新调拨入库")
    public ResultBean<Integer> update(
            @ApiParam(value = "调拨信息", required = true) @RequestBody AllocateInUpVo allocateInUpVo) {
        log.info("更新调拨记录: {}", allocateInUpVo);
        int rows = allocateInService.update(allocateInUpVo);
        return R.saveResult(rows);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "物理删除调拨记录")
    public ResultBean<Integer> deleteById(
            @ApiParam(value = "调拨记录ID", required = true) @PathVariable("id") int id) {
        log.info("物理删除调拨记录: ID={}", id);
        int rows = allocateInService.deleteById(id);
        return R.saveResult(rows);
    }


    @PostMapping("/queryPageAndConditionCallProcess")
    @ApiOperation(value = "调入处理分页查询调拨记录并按条件过滤，返回前端DTO")
    public ResultBean<PageBean<List<CallProcessVO>>> queryPage(
            @ApiParam(value = "调拨查询条件", required = true) @RequestBody CallProcessDTO callProcessDTO) {
        log.info("分页查询调拨记录: 页码={}, 每页条数={}, 条件={}", callProcessDTO.getPage(), callProcessDTO.getPageSize(), callProcessDTO);
        PageBean<List<CallProcessVO>> pageBean = allocateInService.queryProcessPageAndCondition(callProcessDTO);
        return R.success("分页查询成功", pageBean);
    }

    @PutMapping("/updateDetail")
    @ApiOperation(value = "更新调拨处理")
    public ResultBean<Integer> updateDetail(
            @ApiParam(value = "调拨信息", required = true) @RequestBody AllocateInDetailUpVo allocateInDetailUpVo) {
        log.info("更新调拨记录: {}", allocateInDetailUpVo);
        int rows = allocateInService.updateInStoreDetail(allocateInDetailUpVo);
        return R.saveResult(rows);
    }



    @PostMapping("/inStorageType")
    @ApiOperation(value = "调拨入库业务处理")
    public ResultBean<Integer> inStorageType(
            @ApiParam(value = "调拨入库业务处理", required = true) @RequestBody AllocateInTypeVo allocateInTypeVo) {
        log.info("调拨入库业务处理: {}", allocateInTypeVo);

        return R.saveResult(1);
    }
}